<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/app.php');

need_manager();
need_auth('order');

$condition = array(
	'state' => 'pay',
	'team_id > 0',
);

/**
explain SELECT o.* FROM `order` as o left join pin_order_list as po on o.id = po.order_id left join pin_order as pd on po.pin_id=pd.id WHERE  o.state='pay'   and  o.express_no is null and o.is_pin =1 and pd.state =1
**/



/* filter */
$uemail = strval($_GET['uemail']);
if ($uemail) {
	$field = strpos($uemail, '@') ? 'email' : 'username';
	$field = is_numeric($uemail) ? 'id' : $field;
	$uuser = Table::Fetch('user', $uemail, $field);
	if($uuser) $condition['user_id'] = $uuser['id'];
	else $uemail = null;
}

$cbday = strval($_GET['cbday']);
$ceday = strval($_GET['ceday']);
$pbday = strval($_GET['pbday']);
$peday = strval($_GET['peday']);

$is_fahuo = isset($_GET['is_fahuo']) ? intval($_GET['is_fahuo']) : 0;

if($is_fahuo >0)
{
	//is_fahuo = 1  is_fahuo =2
	//$one['express_no']&&$one['express_id']
	if($is_fahuo == 1){
		$condition[] = "express_no !='' and express_id !='' ";
	} else if($is_fahuo == 2){
		$condition[] = "express_no is null ";
	}

}





$o_where = '';

$id = abs(intval($_GET['id']));
 if ($id) {
	//$condition['id'] = $id;
	$o_where .= ' and o.id = '.$id;
}else {
	$id = '';
}
$team_id = abs(intval($_GET['team_id']));
if ($team_id) {
	$o_where .= ' and o.team_id = '.$team_id;
	//$condition['team_id'] = $team_id;
} else { $team_id = null; }


if ($cbday) { 
	$cbtime = strtotime($cbday);
	//$condition[] = "create_time >= '{$cbtime}'";
	
	$o_where .= ' and o.create_time >= "'.$cbtime.'"';
}
if ($ceday) { 
	$cetime = strtotime($ceday);
	//$condition[] = "create_time <= '{$cetime}'";
	$o_where .= ' and o.create_time <= "'.$cetime.'"';
}
if ($pbday) { 
	$pbtime = strtotime($pbday);
	//$condition[] = "pay_time >= '{$pbtime}'";
	$o_where .= ' and o.pay_time >= "'.$pbtime.'"';
}
if ($peday) { 
	$petime = strtotime($peday);
	//$condition[] = "pay_time <= '{$petime}'";
	$o_where .= ' and o.pay_time <= "'.$petime.'"';
}

/* end filter */

$sql = "SELECT count(o.id) as count FROM `order` as o left join pin_order_list as po on o.id = po.order_id left join pin_order as pd on po.pin_id=pd.id WHERE  o.state='pay' ".$o_where."  and  o.express_no is null and o.is_pin =1 and pd.state =1";

$count_arr = DB::GetQueryResult($sql,true);
$count = $count_arr['count'];

list($pagesize, $offset, $pagestring) = pagestring($count, 20);

$order_sql ="SELECT o.* FROM `order` as o left join pin_order_list as po on o.id = po.order_id left join pin_order as pd on po.pin_id=pd.id WHERE  o.state='pay' ".$o_where."  and  o.express_no is null and o.is_pin =1 and pd.state =1 ORDER BY o.pay_time DESC, o.id DESC limit {$offset},{$pagesize}";

if(isset($_GET['daochu']) )
{
	$order_sql ="SELECT o.* FROM `order` as o left join pin_order_list as po on o.id = po.order_id left join pin_order as pd on po.pin_id=pd.id WHERE  o.state='pay' ".$o_where."  and  o.express_no is null and o.is_pin =1 and pd.state =1 ORDER BY o.pay_time DESC, o.id DESC ";
	$orders = DB::GetQueryResult($order_sql,false);
	$list = array();
	
	
	$arcond  = array();
	$arcond['zone'] = 'express'; 
	//DB::Debug();
	$exp_arr = DB::LimitQuery('category', array(
		'condition' => $arcond,
		'order' => 'ORDER BY id DESC',
	));
	$note_str = '备注,系统快递编号对应快递类别是： ';
	foreach($exp_arr as $exp_one)
	{
		$note_str .= $exp_one['id'].'=>'.$exp_one['name'].' ';
	}
	

	$name = '拼团待发货订单信息'.date('Ymd').'.csv';
	$kn = array(
				'id' => '订单号',
				'name' => '姓名',
				'mobile' => '电话',
				'province' => '省',
				'city' => '市',
				'area' => '区',
				'street' => '街道',
				'address' => '完整地址',
				'express_id' => '系统快递编号',
				'expreno' => '快递单号',
				'product' => '货物名称',
				//'shop_country' => '发货国家',
				'shop_province' => '发货省',
				'shop_city' => '发货市',
				'shop_address' => '发货详细地址',
				'service_phone' => '服务电话',
				'note' => $note_str,
				
		);
  
	$kn_str  = implode(',',$kn);
	
	$str = $kn_str."\n"; 
    $str = iconv('utf-8','gb2312',$str); 
	
	foreach($orders as $ex_order)
	{
		if(empty($ex_order['aid']))
		{
			$tp_ad_arr = DB::LimitQuery('address', array(
				'condition' => array('user_id' => $ex_order['user_id']),
			));
			foreach($tp_ad_arr as $vvv)
			{
				$tp_vvv_str = $vvv['province'].$vvv['city'].$vvv['area'].$vvv['street'];
				if($tp_vvv_str == $ex_order['address'])
				{
					$ex_order['aid'] = $vvv['id'];
					break;
				}
			}
		}
		
		$tmp_addre =  Table::Fetch('address', $ex_order['aid']);
		
		$str .= $ex_order['id'].",";
		$str .= iconv('utf-8','gb2312',$tmp_addre['name']).",";
		$str .= ' '.$tmp_addre['mobile']." ,";
		$str .= iconv('utf-8','gb2312',$tmp_addre['province']).",";
		$str .= iconv('utf-8','gb2312',$tmp_addre['city']).",";
		$str .= iconv('utf-8','gb2312',$tmp_addre['area']).",";
		$str .= iconv('utf-8','gb2312',$tmp_addre['street']).",";
		$str .= iconv('utf-8','gb2312',$ex_order['address']).",";
		if(empty($ex_order['express_id']))
			$str .= ' 0 ,';
		else 
			$str .= $ex_order['express_id'].",";
		$str .= ' ,';
		
		$tmp_team =  Table::Fetch('team', $ex_order['team_id']);
		
		$str .= iconv('utf-8','gb2312',$tmp_team['product']).",";
		//$str .= iconv('utf-8','gb2312',$INI['system']['shop_country']).",";
		$str .= iconv('utf-8','gb2312',$INI['system']['shop_province']).",";
		$str .= iconv('utf-8','gb2312',$INI['system']['shop_city']).",";
		$str .= iconv('utf-8','gb2312',$INI['system']['shop_address']).",";
		$str .= iconv('utf-8','gb2312',$INI['system']['service_phone'])."\n";
		
	}
	
	header("Content-type:text/csv"); 
    header("Content-Disposition:attachment;filename=".$name); 
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
    header('Expires:0'); 
    header('Pragma:public'); 
    echo $str; 
	
	die();
}

$orders = DB::GetQueryResult($order_sql,false);
/**
$orders = DB::LimitQuery('order', array(
	'condition' => $condition,
	'order' => 'ORDER BY pay_time DESC, id DESC',
	'size' => $pagesize,
	'offset' => $offset,
));
**/

foreach($orders as $key =>$val)
{
	if($val['is_pin'] == 1)
	{
		//pin_order_list
		$pin_order_list = DB::LimitQuery('pin_order_list', array(
			'condition' => array('order_id'=>$val['id']),
			'one' => true,
		));
		$pin_order =  DB::LimitQuery('pin_order', array(
			'condition' => array('id'=>$pin_order_list['pin_id']),
			'one' => true,
		));
		
		$val['pin_info'] = $pin_order;
		
		$find_pin_order_list = DB::LimitQuery('pin_order_list', array(
			'condition' => array('pin_id'=>$pin_order_list['pin_id']),
			'order' => 'ORDER BY createtime ASC',
			'one' => true,
		));
		
		$tmp_order = Table::Fetch('order', $find_pin_order_list['order_id']);	
		$tmp_user = Table::Fetch('user', $tmp_order['user_id']);	
		
		//id val  user_id 
		if($tmp_user['id'] == $val['user_id']){
			$val['is_tuanz'] = true;
		} else {
			$val['is_tuanz'] = false;
			$val['tuan_info'] = $tmp_user;
			$val['tuan_order_id'] = $find_pin_order_list['order_id'];
		}
		$orders[$key] = $val;
	}
}

$pay_ids = Utility::GetColumn($orders, 'pay_id');
$pays = Table::Fetch('pay', $pay_ids);

$user_ids = Utility::GetColumn($orders, 'user_id');
$users = Table::Fetch('user', $user_ids);

$team_ids = Utility::GetColumn($orders, 'team_id');
$teams = Table::Fetch('team', $team_ids);

include template('manage_order_pay_pin');
